package com.cskaoyan.user.service.impl;

import com.cskaoyan.user.constants.DBaffectedRows;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserRegisterMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.email.SendEmailThread;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.service.IUserRegisterService;
import com.cskaoyan.user.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class UserRegisterServiceImpl implements IUserRegisterService {


    @Autowired
    UserRegisterMapper userRegisterMapper;
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    SendEmailThread sendEmailThread;
    @Autowired
    MemberMapper memberMapper;

    /**
     * @Author Chen
     * @Date 2022-09-20  20-59-50
     * @param
     * @return {@link null }
     * @description 新用户注册
     */
    @Transactional
    @Override
    public UserRetCode register(UserRegisterRequest userRegister) {


        UserVerify userVerify = new UserVerify();
        userVerify.setUsername(userRegister.getUserName());
        userVerify.setRegisterDate(new Date());
        String uuid = UUID.randomUUID().toString().replace("-", "");
        userVerify.setUuid(uuid);
        userVerify.setIsVerify("N");
        userVerify.setIsExpire("N");

        userVerifyMapper.insertSelective(userVerify);


        Member member = new Member();

        member.setUsername(userRegister.getUserName());
        member.setEmail(userRegister.getEmail());

        String encryptPassword = MD5Utils.encryptPassword(userRegister.getUserPwd(), userRegister.getUserName());
        member.setPassword(encryptPassword);

        member.setCreated(new Date());
        member.setUpdated(new Date());

        int addResult = userRegisterMapper.insertSelective(member);


        if (addResult== DBaffectedRows.ONE){

            sendEmailThread.setReceiver(userRegister.getEmail());
            sendEmailThread.setSubject("恭喜您成为第8888位用户，赶快激活使用吧！");
            sendEmailThread.setContent("<a href="+"'http://127.0.0.1:8080/user/verify?username="+userRegister.getUserName()+"&uuid="+uuid+"'>"+"尊敬的用户："+userRegister.getUserName()+" 点击激活您的账户"+"</a>");
            Thread sendEmail = new Thread(sendEmailThread);
            sendEmail.start();
            return UserRetCode.SUCCESS;
        }
        return UserRetCode.USERNAME_ALREADY_EXISTS;
    }

    /**
     * @Author Chen
     * @Date 2022-09-20  21-38-17
     * @param
     * @return {@link null }
     * @description  新用户激活账户
     */
    @Transactional
    @Override
    public UserRetCode verify(UserVerifyRequest userVerify) {


        Example example = new Example(UserVerify.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("username",userVerify.getUsername());
        criteria.andEqualTo("uuid",userVerify.getUuid());

        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);

        if (userVerifies.size()==DBaffectedRows.ONE){

            UserVerify verify = userVerifies.get(0);
            verify.setIsVerify("Y");

            userVerifyMapper.updateByPrimaryKey(verify);

            Member member = new Member();
            member.setIsVerified("Y");

            Example memberExample = new Example(Member.class);
            Example.Criteria memberExampleCriteria = memberExample.createCriteria();
            memberExampleCriteria.andEqualTo("username",verify.getUsername());
            memberMapper.updateByExampleSelective(member,memberExample);

            return UserRetCode.SUCCESS;
        }

        return UserRetCode.USERVERIFY_INFOR_INVALID;
    }
}
